package com.tuanzi.loan.business.repository.statistics.impl;

import com.tuanzi.loan.business.entity.statistics.AdverChannelTypeUser;
import com.tuanzi.loan.business.repository.collaborate.custom.AdverChannelTypeUserRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepository;
import com.tuanzi.loan.vo.request.Collaborate.CollaborateAdvertiseConfigListRequest;
import com.tuanzi.loan.vo.response.collaborate.CollaborateAdvertiseConfigListResponse;
import org.apache.commons.lang3.StringUtils;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import java.util.List;

import static com.tuanzi.loan.generated.jooq.loan.tables.AdverChannelTypeUser.ADVER_CHANNEL_TYPE_USER;


/**
 * @author Simon
 * @date 2019/07/29 14:28
 */
@Repository
public class AdverChannelTypeUserRepositoryImpl extends JOOQBaseRepository implements AdverChannelTypeUserRepositoryCustom {

    protected TableImpl getTable() {
        return ADVER_CHANNEL_TYPE_USER;
    }

    public Page<CollaborateAdvertiseConfigListResponse> list(CollaborateAdvertiseConfigListRequest searchRequest, Pageable pageable) {
        fields(ADVER_CHANNEL_TYPE_USER.fields());

        /*String[] channels = searchRequest.getChannels();
        if (channels != null && channels.length > 0) {
            List<String> collect = Arrays.stream(channels).collect(Collectors.toList());
            in(ADVER_CHANNEL_TYPE_USER.CHANNEL, collect);
        }*/
        if(searchRequest.getChannel() != null && !"1000".equals(searchRequest.getChannel())){
            eq(ADVER_CHANNEL_TYPE_USER.CHANNEL, searchRequest.getChannel());
        }
        if(StringUtils.isNotBlank(searchRequest.getAccount()) && StringUtils.isNotEmpty(searchRequest.getAccount())){
            contains(ADVER_CHANNEL_TYPE_USER.USER_NAME, searchRequest.getAccount());
        }
        if(searchRequest.getAdsPlatformType() != null && 1000 != searchRequest.getAdsPlatformType()){
            eq(ADVER_CHANNEL_TYPE_USER.ADS_PLATFORM_TYPE,searchRequest.getAdsPlatformType());
        }

        return search(pageable, record -> {
            CollaborateAdvertiseConfigListResponse response = new CollaborateAdvertiseConfigListResponse();
            response.setId(record.get(ADVER_CHANNEL_TYPE_USER.ID));
            response.setAccount(record.get(ADVER_CHANNEL_TYPE_USER.USER_NAME));
            response.setChannel(record.get(ADVER_CHANNEL_TYPE_USER.CHANNEL));
            response.setAdsPlatformType(record.get(ADVER_CHANNEL_TYPE_USER.ADS_PLATFORM_TYPE));
            return response;
        });
    }

    public AdverChannelTypeUser findOne(Long id) {
        return dslContext.select(ADVER_CHANNEL_TYPE_USER.fields()).from(ADVER_CHANNEL_TYPE_USER).where(ADVER_CHANNEL_TYPE_USER.ID.eq(id)).fetchOneInto(AdverChannelTypeUser.class);
    }

    public List<AdverChannelTypeUser> findCurrentUserChannels(String account) {
        return dslContext.select(ADVER_CHANNEL_TYPE_USER.fields()).from(ADVER_CHANNEL_TYPE_USER).where(ADVER_CHANNEL_TYPE_USER.USER_NAME.eq(account)).fetchInto(AdverChannelTypeUser.class);
    }

    public void delete(String account) {
        dslContext.delete(ADVER_CHANNEL_TYPE_USER).where(ADVER_CHANNEL_TYPE_USER.USER_NAME.eq(account)).execute();
    }

}
